 package com.es.pay.grade.action;
 
 import java.io.FileInputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import com.es.pay.grade.server.StorefrontGradeImport;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.xwork.ActionSupport;
 
 
public class ImportStorefrontGradeExcelListAction extends ActionSupport implements ServletRequestAware{
	 
	HttpServletRequest request;
 

public String execute() throws SQLException, Exception {
     request.setCharacterEncoding("utf-8");
     String intime = request.getAttribute("intime").toString();
     String seesionId = request.getSession().getId()+System.currentTimeMillis();
     String message ="";
     StorefrontGradeImport biz = new StorefrontGradeImport();
     try {
       String path = System.getProperty("appDir") + "upload\\" + (String)request.getAttribute("fileName");
 
       FileInputStream in = new FileInputStream(path);
       Workbook rwb = Workbook.getWorkbook(in); 
       Sheet sheet = rwb.getSheet(0); 
       int colCount = sheet.getColumns();
       int rowCount = sheet.getRows();
       
       ArrayList<ArrayList> queryParms = new ArrayList<ArrayList>();
       Cell temp = null;
       for (int row = 1; row < rowCount; ++row){
    	   ArrayList<String> rowCell = new ArrayList<String>();
    	   for (int col = 0; col < colCount; ++col) {
    		   temp = sheet.getCell(col, row); 
    		   if(col == 0&&sheet.getCell(0, row)!=null&&sheet.getCell(0, row).getContents().equals("")){
    			   break;    			   
    		   }
    		   if (temp!=null) {
    			   rowCell.add(temp.getContents());
    		   }
    	   }
    	   if(rowCell.size()>0){
    		   rowCell.add(String.valueOf(row));
    		   rowCell.add(seesionId);
    		   rowCell.add(intime);
    		   queryParms.add(rowCell);
    	   }
    	   
    	   //每5000次提交一次 做批量提交，防止大数据内存溢出
    	   if(queryParms.size()%5000==0){
    		   if(!biz.importStorefrontGradeExcelList(queryParms)){
    			   message = "文件导入失败！请确定是否是正确的门店信息模板。";
    			   request.setAttribute("message", message);
    			   return "false";
    		   }
    		   queryParms.clear();
    	   }    	  
       }
       if(queryParms.size()>0){
	       biz.importStorefrontGradeExcelList(queryParms);
		   queryParms.clear();
       }
       
       ArrayList<HashMap<String, Object>> map =biz.importStorefrontGradeExcelListDo(seesionId, intime);
       if(map.size()==1&&map.get(0).containsKey("rowNum")){
    	   message = "文件导入成功！共导入"+map.get(0).get("rowNum")+"条数据！";
           request.setAttribute("message", message);
           return "success";    	   
       }else{
    	   request.setAttribute("errorMsg", map);
    	   message = "文件导入失败！存在数据验证错误。";
 		   request.setAttribute("message", message);
    	   return "errorMsg";
       }
     }catch(Exception e){
    	  message = "文件导入失败！请重新尝试。";
		  request.setAttribute("message", message);
		  return "false";
     }
       
   }
 
   public void setServletRequest(HttpServletRequest request) {
     this.request = request;
   }
 }

